package com.syl.starter.itools.framework.utils.help;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.syl.starter.itools.base.utils.LogUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author syl
 * @create 2019-05-13 10:02
 **/
public class BaseXMapper {
    private static final Logger LOG = LoggerFactory.getLogger(BaseXMapper.class);

    /**
     * 通用的 XX ToString
     * @param mapper
     * @param object
     * @param strategy
     * @return
     */
    public static String xToString(ObjectMapper mapper, Object object, JsonInclude.Include strategy){
        if(object == null)
            return null;
        if(strategy != null)
            mapper.setSerializationInclusion(strategy);
        try {
            return mapper.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            LogUtils.error(LOG,"转换失败", e);
        }
        return null;
    }

}
